import React from "react";
import {Button,  Divider,  Modal} from 'antd/lib';
import SuspendTable from "@/components/contract/SuspendTable";
import ContractSuspend from "@/services/contractsuspend";
import notification from "@/services/notification";
import {get} from "lodash";
import CheckDialog from "@/components/workflow/CheckDialog";
import {currentUser} from "@/services/auth";
import Workflow from "@/services/workflow";
import moment from "moment";
import WarningOutlined from "@ant-design/icons/WarningOutlined";
import Contracts from "@/services/contracts";
import GlobalFunctions from '@/store/globalFunctions';
import {createBrowserHistory} from 'history';
import { inject, observer } from 'mobx-react';
const customHistory = createBrowserHistory();
const { confirm } = Modal;
class SuspendToolbar extends React.Component {
  state = {
    loading: true,
  };
  componentWillMount() {
    this.refreshData(this.props.id);
    this.setState({
      loading: false,
    });
  }
  refreshData(id = 0) {
    this.props.contractSuspendStore.initStore(id);
}
  handleadd = () => {
    this.refreshData(0);
  };
  handleSave = () => {

    this.props.contractSuspendStore.dataSave();
  };
  handleDelete = () => {
    if(this.props.contractSuspendStore.contractSuspendModel.status !==0){
      notification.error("不是草稿状态，不能删除！");
      return;
    }
    confirm({
      title: '确定删除合同中止申请单申请单?',
      icon: <WarningOutlined />,
      content: this.props.contractSuspendStore.contractSuspendModel.name,
      okText: '确定',
      okType: 'danger',
      cancelText: '取消',
      onOk: () => {
        this.props.contractSuspendStore.dataDelete();
        this.refreshData(0);
      },
    });
  };

  handlebackpage= () => {
    customHistory.goBack();
  };
  handleCheck = () => {
    this.props.contractSuspendStore.billCheck(currentUser.id);
  };
  render() {
    if (this.state.loading) return null;
    return(
      <div>
        <div style={{ marginBottom: 16 }}>
        <Button type="primary" style={{margin:"10px"}} onClick={() => this.handlebackpage()}>
            <i className="fa fa-reply m-r-5" />
            返回
          </Button>
          <Divider type="vertical" />
          <Button type="primary" onClick={() => this.handleadd()}>
            <i className="fa fa-plus m-r-5" />
            新增
          </Button>
          <Divider type="vertical" />
          <Button type="primary" onClick={() => this.handleSave()} disabled ={!this.props.contractSuspendStore.contractSuspendModel.isedit}>
            <i className="fa fa-save m-r-5" />
            保存
          </Button>
          <Divider type="vertical" />
          <Button type="primary" onClick={() => this.handleDelete()} disabled ={this.props.contractSuspendStore.contractSuspendModel.status !==0}>
            <i className="fa fa-trash m-r-5" />
            删除
          </Button>
          <Divider type="vertical" />
          <Button type="primary" onClick={() => this.handleCheck()} disabled ={this.props.contractSuspendStore.contractSuspendModel.status===2 ? true:false}>
            <i className="fa fa-check m-r-5" />
            {this.props.contractSuspendStore.contractSuspendModel.status===0 ? "提交":"审核"}
          </Button>
        </div>
        <div>
          <SuspendTable  id={this.props.id} />
        </div>
      </div>
    );
  };
}

export default inject('contractSuspendStore')(observer(SuspendToolbar));